from flask import Flask,render_template,url_for,make_response,request,session
app=Flask(__name__)
app.secret_key='dsjfjosfj'
# 可以设置 app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hour=2) 在两个小时后过期
@app.route('/')
def test():
    # url('主名.方法')
    # print(url_for('user.index'))
    return render_template('main.html')
@app.route('/make_cookie/')
def make_cookie():
    cook=make_response('set cookie ok')
    age=60*60*2
    # cookie以键值对数据存储,max_age时长以秒计算,expires以指定时间为cookie的有效期
    # tmp_time=datatime(2021,11,11,hour=18.minute=0,second=0)
    # 若两个都设置以max_age为准

    cook.set_cookie('uname','lxh',max_age=age)
    return render_template('make_cookie.html',cook=cook)
@app.route('/get_cookie/')
def get_cookie():
    # 获取cookie
    get=request.cookies.get('uname')
    if get:
        return render_template('get_cookie.html',get=get)
    else:return '已经没有cookie了'

@app.route('/del_cookie/')
def del_cookie():
    resp=make_response('Hello del')
    # 以键的形式删除cookie
    resp.delete_cookie('uname')
    print('删除了')
    return resp

@app.route('/set_session/')
def set_session():
    # session.permanent()设置session持久化，默认为31天  
    session['uname']='shxn'
    return 'set session ok'

@app.route('/get_session/')
def get_session():
    uname=session.get('uname')
    return f'get session {uname}'

@app.route('/del_session/')
def delete_session():
    # 删除指定key的session
    # session.pop('uname')
    # 删除所有session
    session.clear()
    return '删除成功'
from user import user_bp
from good import good_bp
app.register_blueprint(user_bp)
app.register_blueprint(good_bp)
if __name__=="__main__":
    app.run(debug=True)